/*
 * @Author: gitee_com_zb
 * @Date: 2024-12-10 14:58:33
 * @LastEditors: gitee_com_zb
 * @LastEditTime: 2024-12-10 14:58:49
 * @FilePath: /algorithm/每日一题10.两个数组的交集(easy).cpp
 * @Description: 题目链接 https://leetcode.cn/problems/intersection-of-two-arrays
 */
class Solution {
public:
    // vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    //     std::set<int> s1(nums1.begin(),nums1.end());
    //     std::set<int> s2(nums2.begin(),nums2.end());
    //     std::set<int>::iterator it1=s1.begin();
    //     std::set<int>::iterator it2=s2.begin();
    //     vector<int> ret;
    //     while(it1!=s1.end() && it2!=s2.end())
    //     {
    //         if(*it1<*it2) ++it1;
    //         else if(*it2<*it1) ++it2;
    //         else {
    //             ret.push_back(*it1);
    //             ++it1,++it2;
    //         }
    //     }
    //     return ret;
    // }
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        // 排序+双指针
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        int p1 = 0, p2 = 0;
        vector<int> ret;
        while(p1 < nums1.size() && p2 < nums2.size()) {
            if (nums1[p1] == nums2[p2]) {
                if(ret.empty() || nums1[p1] != ret.back())
                    ret.push_back(nums1[p1]);
                p1++, p2++;
            }
            else if (nums1[p1] < nums2[p2]) p1++;
            else p2++;
        }
        return ret;
    }
};